package com.mini.web.controller.front;

import com.mini.common.annotation.Anonymous;
import com.mini.common.core.controller.BaseController;
import com.mini.common.core.domain.AjaxResult;
import com.mini.common.core.domain.entity.SysUser;
import com.mini.common.core.domain.model.LoginUser;
import com.mini.framework.web.service.TokenService;
import com.mini.system.domain.dto.LoginDTO;
import com.mini.system.domain.dto.RegisterDTO;
import com.mini.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mini
 * 2025.06.30
 */
@Api(tags = "用户相关接口")
@RequestMapping("/front")
@RestController
@RequiredArgsConstructor
@Slf4j
public class FrontController  extends BaseController {

    private final SysUserMapper sysUserMapper;
    private final PasswordEncoder passwordEncoder;
    private final TokenService tokenService;

    @Anonymous
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public AjaxResult login(@Validated @RequestBody LoginDTO loginDTO) {
        log.info("用户登录: {}", loginDTO.getUsername());

        // 查询用户
        SysUser user = sysUserMapper.selectUserByUserName(loginDTO.getUsername());
        if (user == null) {
            return error("用户不存在");
        }

        // 验证密码
        if (!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
            return error("用户名或密码错误");
        }

        // 生成token
        String token = tokenService.createToken(new LoginUser(user));

        // 返回用户信息
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("user", user);

        return success(data);
    }

    @Anonymous
    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public AjaxResult register(@Validated @RequestBody RegisterDTO registerDTO) {
        log.info("用户注册: {}", registerDTO.getUsername());

        // 验证两次密码是否一致
        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
            return error("两次输入的密码不一致");
        }

        // 检查用户名是否已存在
        if (sysUserMapper.checkUserNameUnique(registerDTO.getUsername()) != null) {
            return error("用户名已存在");
        }

        // 创建用户对象
        SysUser user = new SysUser();
        user.setUserName(registerDTO.getUsername());
        // 加密密码
        user.setPassword(passwordEncoder.encode(registerDTO.getPassword()));
        user.setNickName(registerDTO.getUsername());
        user.setStatus("0"); // 正常状态

        // 保存用户
        sysUserMapper.insertUser(user);

        return success();
    }
}
